<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        textarea {
            width: 200px;
            height: 100px;
            outline: none;
            resize: none;
            margin-left: 100px;
            margin-top: 100px;
            vertical-align: sub;
        }

        li {
            width: 200px;
            height: 30px;
            line-height: 30px;
            margin: 10px 0 0 100px;
            border: 1px solid red;
            background-color: rgb(245, 209, 243);
            list-style: none;
            font-size: 14px;
        }

        li a {
            float: right;
            padding: 0 5px;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <textarea placeholder="请输入您要发表的留言" class="tex"></textarea>
    <button class="btn">发布</button>
    <ul>
    </ul>
    <script>
        var tex = document.querySelector('.tex');
        var btns = document.querySelector('.btn');
        var ul = document.querySelector('ul');
        btns.onclick = function () {
            //添加元素
            if (tex.value == '') {
                alert('您没有输入内容!')
                return false
            } else {
                var li = document.createElement('li');
                li.innerHTML = tex.value + '<a href = "javascript:;">删除</a>';
                ul.insertBefore(li, ul.children[0]);

                //删除元素
                var as = document.querySelectorAll('a');
                for (var i = 0; i < as.length; i++) {
                    as[i].onclick = function () {
                        ul.removeChild(this.parentNode);
                    }
                }
            }
        }
    </script>
</body>

</html>